package com.my.cachedemo.controller;

import com.my.cachedemo.bean.User;
import com.my.cachedemo.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 缓存测试
 *
 * @author 王昊然
 * @create 2018-12-28 19:00
 **/
@Slf4j
@RestController
public class CacheController {

    @Resource
    private CacheService cacheService;

    @Resource
    private CacheManager caffeineCacheManager;

    /**
     * default
     * org.springframework.cache.concurrent.ConcurrentMapCache
     *
     * @param id
     * @date 2018/12/28 19:27
     * @author 王昊然
     */
    @GetMapping("/cache")
    public HttpEntity<User> testCache(@RequestParam int id) {
        User user = cacheService.getUser(id);
        return new HttpEntity<>(user);
    }

    @GetMapping("/cacheManager")
    public String cacheManager(String cacheName) {
        log.info("cache对象信息：{}, {}", caffeineCacheManager.getCache(cacheName)
            .toString());
        return caffeineCacheManager.getCacheNames()
            .toString();
    }
}
